<?php

namespace app\common\model;

use think\Model;

class BaseModel extends Model
{
	/**
	 * 获取单条记录
	 * @Author   zhibin
	 * @DateTime 2019-11-21
	 * @param    array      $where   [description]
	 * @param    string     $field [description]
	 * @return   [type]            [description]
	 */
	public function getInfo(array $where=[], $field='*')
	{
		$info = $this->where($where)->field($field)->find();
		// echo $this->getLastSql();die;
		$info && $info = $info->toArray();

		return $info;
	}

	/**
	 * 查询列表
	 * @Author   zhibin
	 * @DateTime 2019-11-18
	 * @param    array      $where     [description]
	 * @param    integer    $nowPage [description]
	 * @param    integer    $limits  [description]
	 * @return   [type]              [description]
	 */
	public function getList(array $where=[], $field='*', $order='id desc')
	{
		$list = $this->where($where)->field($field)->order($order)->select();
		$list = json_decode(json_encode($list), true);

		return $list;
	}

	/**
	 * 查询列表
	 * @Author   zhibin
	 * @DateTime 2019-11-18
	 * @param    array      $where     [description]
	 * @param    integer    $nowPage   [description]
	 * @param    integer    $limits    [description]
	 * @return   [type]                [description]
	 */
	public function getPageList(array $where=[], $page=1, $limit=10, $field='*', $order='id desc')
	{
		$pageList = $this->where($where)->field($field)->order($order)->paginate($limit);
		$page = $pageList->render();
		$pageList = json_decode(json_encode($pageList), true);
		$pageList['page'] = $page;

		return $pageList;
	}

	
	/**
	 * 单条数据
	 * @Author   zhibin
	 * @DateTime 2019-12-08
	 * @param    array      $where [description]
	 * @param    array      $data  [description]
	 * @return   [type]            [description]
	 */
	public function add(array $data)
	{
		$res = $this->insert($data);

		return $res;
	}

	/**
	 * 单条数据
	 * @Author   zhibin
	 * @DateTime 2019-12-08
	 * @param    array      $where [description]
	 * @param    array      $data  [description]
	 * @return   [type]            [description]
	 */
	public function edit(array $where, array $data)
	{
		$res = $this->where($where)->update($data);

		return $res;
	}

	/**
	 * 计算总数
	 * @Author   zhibin
	 * @DateTime 2020-02-16
	 * @param    array      $map [description]
	 * @return   [type]          [description]
	 */
	public function getCount(array $map=[])
	{
		$count = $this->where($map)->count();

		return $count;
	}
}